package com.sd.repay.transfer;

import com.sd.repay.pojo.TransferChannelInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class TransferChannelCache {


    private static TransferService transferService;

    @Autowired
    private TransferService transferService2;

    private static Map<String, Object> cacheTransferChanel = new ConcurrentHashMap<String, Object>();

    @Autowired
    TransferChannelCache(TransferService transferService) {
        TransferChannelCache.transferService = transferService;
    }

    @PostConstruct
    public void beforeInit() {
        transferService = transferService2;
    }

    public static void getTransferChannel() {
        List<TransferChannelInfo> channelList = transferService.getTransferChannelInfoList();
        for (TransferChannelInfo infoTemp : channelList) {
            cacheTransferChanel.put(infoTemp.getBankCode() + infoTemp.getBankMerchantNo(), infoTemp);
        }
    }

    public static Object getChannel(String channelKey) {
        Object channelObj = cacheTransferChanel.get(channelKey);
        if (channelObj == null) {
            getTransferChannel();
            channelObj = cacheTransferChanel.get(channelKey);
        }

        return channelObj;

    }

}
